package com.qinglei.recoup.system.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.exception.RedisConnectException;
import com.qinglei.recoup.common.service.RedisService;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.system.domain.HealthBigData;
import com.qinglei.recoup.system.domain.VitalSignsAlarm;
import com.qinglei.recoup.system.service.HealthBigDataService;
import com.qinglei.recoup.system.service.VitalSignsAlarmService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author dqh
 */
@RestController
@RequestMapping("/health-big-data")
public class HealthBigDataController {


    @Resource
    private HealthBigDataService healthBigDataService;

    @Resource
    private RedisService redisService;

    @PostMapping("/select")
    @Transactional(rollbackFor = Exception.class)
    public CommonRes selectHealthBigData(@RequestBody Map<String, Object> map) throws Exception {
        //重置默认值
        List<HealthBigData> list = healthBigDataService.list(Wrappers.
                <HealthBigData>lambdaQuery()
                .eq(HealthBigData::getTenantId, map.get("tenantId")));
        return CommonRes.ok(list);
    }

    @PostMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public CommonRes updateHealthBigData(@RequestBody List<HealthBigData> healthBigDataList) throws Exception {
        //修改
        healthBigDataService.updateBatchById(healthBigDataList);

        List<HealthBigData> HealthBigDataList2 = healthBigDataService.list(Wrappers.<HealthBigData>lambdaQuery().eq(HealthBigData::getTenantId, healthBigDataList.get(0).getTenantId()));
        HealthBigDataList2.forEach(f->{
            String s = JsonUtils.objectToJson(f);
            try {
                redisService.set("HealthBigData-"+f.getTenantId()+"-"+f.getBigDataType(),s);
            } catch (RedisConnectException e) {
                e.printStackTrace();
            }

        });

        return CommonRes.ok("修改成功");
    }

}
